package com.fh.fruit.controller.activity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.fruit.service.activity.ActivityManager;
import com.fh.util.DateUtil;
import com.fh.util.Jurisdiction;
import com.fh.util.ObjectExcelView;
import com.fh.util.PageData;

@Controller
@RequestMapping(value = "/mult")
public class ActivityMultController extends BaseController {
	String menuUrl = "mult/listActivity.do"; // 地址(权限用)

	@Resource(name = "activityService")
	private ActivityManager activityManager;
	
	/**多级活动邀请统计
	 * @param page
	 * @param httpSession
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping(value="/listActivity")
	public ModelAndView listActivity(Page page,HttpSession httpSession)throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		page.setPd(pd);
		List<Map<String,Object>> section=activityManager.getGradeSection();
		List<Map<String,Object>> activityList = activityManager.getListActivity(page);
		pd.put("num", page.getTotalResult());
		pd.put("section", section);
		mv.setViewName("farm/mult/mult_list");
		mv.addObject("activityList", activityList);
		mv.addObject("pd", pd); 
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		return mv;
	}
	
	
	/**导出用户注册信息到EXCEL
	 * @return
	 */
	@RequestMapping(value="/excel")
	public ModelAndView exportExcel(Page page){
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		try{
			if(Jurisdiction.buttonJurisdiction(menuUrl, "cha")){
				
				Map<String,Object> dataMap = new HashMap<String,Object>();
				List<String> titles = new ArrayList<String>();
				titles.add("UID"); 		//1
				titles.add("昵称");  		//2
				titles.add("手机号");			//3
				titles.add("直推");			//4
				titles.add("间推");			//5
				titles.add("等级");			//6
				titles.add("获得兑换券");		//7
				titles.add("活动现金总收入");		//8
				titles.add("最近一次下单时间");		//9
				dataMap.put("titles", titles);
				page.setShowCount(Integer.parseInt(pd.getString("showCount")));
				page.setCurrentPage(Integer.parseInt(pd.getString("pageNum")));
				page.setPd(pd);
				List<Map<String,Object>> activityList = activityManager.getListActivity(page);
				List<PageData> varList = new ArrayList<PageData>();
				for(int i=0;i<activityList.size();i++){
					PageData vpd = new PageData();
					String time=(String)activityList.get(i).get("time");
					if(time ==null || "".equals(time)){
						time="-";
					}
					vpd.put("var1", (Integer)activityList.get(i).get("userId"));		//1
					vpd.put("var2", (String)activityList.get(i).get("petName"));		//2
					vpd.put("var3", (String)activityList.get(i).get("phone"));			//3
					vpd.put("var4", (Long)activityList.get(i).get("direct"));	//4
					vpd.put("var5", (Long)activityList.get(i).get("indirect"));		//5
					vpd.put("var6", (Integer)activityList.get(i).get("grade"));		//6
					vpd.put("var7", (Integer)activityList.get(i).get("repeat_number"));	//7
					vpd.put("var8", (BigDecimal)activityList.get(i).get("income"));	//8
					vpd.put("var9", time);	//9
					varList.add(vpd);
				}
				dataMap.put("varList", varList);
				ObjectExcelView erv = new ObjectExcelView();					//执行excel操作
				mv = new ModelAndView(erv,dataMap);
			}
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	/**多级售出商品统计
	 * @param page
	 * @param httpSession
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping(value="/multGoods")
	public ModelAndView multGoods(Page page,HttpSession httpSession)throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		page.setPd(pd);
		List<Map<String,Object>> multGoods = activityManager.getMultGoods(page);
	//	List<Map<String,Object>> multGoods1 = activityManager.getMultGoods(pd);
		long num=0;
		 BigDecimal price=new BigDecimal(0.00); 
		if(multGoods != null){
			for (Map<String, Object> map : multGoods) {
				num+=(long)map.get("number");
				price=price.add((BigDecimal)map.get("real_price"));
			}
		}
		pd.put("num", num);
		pd.put("price", price.setScale(2, BigDecimal.ROUND_DOWN));
		mv.setViewName("farm/mult/mult_goods_list");
		mv.addObject("multGoods", multGoods);
		mv.addObject("pd", pd); 
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		return mv;
	}
	
	
	/**导出多级出售商品信息到EXCEL
	 * @return
	 */
	@RequestMapping(value="/multGoodsExcel")
	public ModelAndView workExcel(Page page){
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		try{
			if(Jurisdiction.buttonJurisdiction(menuUrl, "cha")){
				Map<String,Object> dataMap = new HashMap<String,Object>();
				List<String> titles = new ArrayList<String>();
				titles.add("ID"); 		//1
				titles.add("期数");  	//2
				titles.add("商品名");		//3
				titles.add("下单时间");	//4
				titles.add("总金额 (元)");	//5
				titles.add("件数");      //6
				titles.add("用户昵称");	//7
				titles.add("手机号");		//8
				dataMap.put("titles", titles);
				page.setShowCount(Integer.parseInt(pd.getString("showCount")));
				page.setCurrentPage(Integer.parseInt(pd.getString("pageNum")));
				page.setPd(pd);
				List<Map<String,Object>> multGoods = activityManager.getMultGoods(page);
				List<PageData> varList = new ArrayList<PageData>();
				for(int i=0;i<multGoods.size();i++){
					BigDecimal bd =(BigDecimal)multGoods.get(i).get("real_price");
					if(bd != null){
						bd=bd.setScale(2,BigDecimal.ROUND_HALF_UP);
					} 
					PageData vpd = new PageData();
					vpd.put("var1", (Integer)multGoods.get(i).get("goods_id"));		//1
					vpd.put("var2", (String)multGoods.get(i).get("period")+ " 期");		//2
					vpd.put("var3", (String)multGoods.get(i).get("title"));			//3
					vpd.put("var4", (String)multGoods.get(i).get("create_time"));	//4
					vpd.put("var5", bd);		//5
					vpd.put("var6", (Integer)multGoods.get(i).get("number"));		//6
					vpd.put("var7", (String)multGoods.get(i).get("petName"));		//7
					vpd.put("var8", (String)multGoods.get(i).get("phone"));	//8
					varList.add(vpd);
				}
				dataMap.put("varList", varList);
				ObjectExcelView erv = new ObjectExcelView();					//执行excel操作
				mv = new ModelAndView(erv,dataMap);
			}
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	/**多级活动统计
	 * @param page
	 * @param httpSession
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping(value="/multDetails")
	public ModelAndView wholeDetails(Page page,HttpSession httpSession)throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		String times=pd.getString("times");
		if(times != null && !"".equals(times)){
			if("1".equals(times)){
				pd.put("startTime", DateUtil.getDay());
				pd.put("endTime", DateUtil.getTime());
			} else if("2".equals(times)){
				pd.put("startTime", DateUtil.getAfterDayDates("-1"));
				pd.put("endTime", DateUtil.getAfterDayDates("-1"));
			} else if("3".equals(times)){
				pd.put("startTime", DateUtil.getAfterDayDates("-2"));
				pd.put("endTime", DateUtil.getAfterDayDates("-2"));
			} else if("4".equals(times)){
				pd.put("startTime", DateUtil.getAfterDayDates("-7"));
				pd.put("endTime", DateUtil.getTime());
			} else if("5".equals(times)){
				pd.put("startTime", DateUtil.getAfterDayDates("-30"));
				pd.put("endTime", DateUtil.getTime());
			}
		}
		String endTime=pd.getString("endTime");
		if(endTime != null && !"".equals(endTime)){
			pd.put("endTime", DateUtil.getSpecifiedDayBefore(pd.getString("endTime")));
		}
		Map<String,Object> cont = activityManager.getMultCount(pd);
		if(cont!=null){
			BigDecimal bd=(BigDecimal)cont.get("price");
			cont.put("price", bd.setScale(2,BigDecimal.ROUND_HALF_UP));
		}
		List<Map<String,Object>> multGoods = activityManager.getMultGoodsCount(pd);
		pd.put("endTime", endTime);
		mv.setViewName("farm/mult/activity-statistics");
		mv.addObject("pd", pd); 
		mv.addObject("cont", cont); 
		mv.addObject("multGoods", multGoods); 
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		return mv;
	}
	
	/**奖金明细
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping(value="/toBonus")
	public ModelAndView toBonus(Page page)throws Exception{
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "cha")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		page.setPd(pd);
		Map<String,Object> user=activityManager.getUserDetails(pd);
		List<Map<String,Object>> bonus = activityManager.getBonusList(page);
		mv.addObject("user",user);
		mv.setViewName("farm/mult/bouns_detail");
		mv.addObject("pd", pd);
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		return mv;
	}
}
